package org.ithang.application.bcAddr;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.ithang.application.bcAddr.bean.BcAddr;
import org.ithang.application.bcAddr.service.BcAddrService;
import org.ithang.system.keyvalue.bean.Keyvalue;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.eth.ETHUtil;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ActionValues;
import org.ithang.tools.model.Pager;
import org.ithang.tools.redis.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 充币地址管理
 * @author zyt 控制器
 *
 */
@Controller
@RequestMapping("/app/bcAddr")
public class BcAddrAction extends Action<BcAddr>{

    @Autowired
    private BcAddrService bcAddrService; 
    
    @Autowired
    private KeyvalueService keyvalueService;


    @ResponseBody
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ActionResult add(BcAddr bcAddr){
		bcAddrService.add(bcAddr);
		return success();
	}
	
	 @RequestMapping("form")
    public String form(@RequestParam(value="id",required=false)Integer id,Model m){
    	if(null!=id){
    		BcAddr bean=bcAddrService.get(id);
    		m.addAttribute(bean);
    	}else{
    		m.addAttribute(new BcAddr());
    	}
    	return "app/bcAddr/form";
    }
	
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(BcAddr bcAddr){
		bcAddrService.update(bcAddr);
		return success();
	}


    @ResponseBody
	@RequestMapping(value="get",method=RequestMethod.GET)
	public ActionResult get(Integer id){
		BcAddr r=bcAddrService.get(id);
		return success(r);
	}
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ActionResult delete(@RequestParam(value="id",required=false)Integer id,@RequestParam(value="ids",required=false)String ids){
		
		if(null!=id&&id>0){
			bcAddrService.delete(id);	
		}
		
		if(null!=ids&&ids.length()>0){
			bcAddrService.batchDelete(ids.split(","));
		}
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="list",method={RequestMethod.GET,RequestMethod.POST})
	public ActionResult list(Integer... ids){
		return success(bcAddrService.list(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ActionResult page(BcAddr bcAddr,Pager<BcAddr> page){
	    page.setBean(bcAddr);
	    List<BcAddr> data=bcAddrService.page(page);
	    page.setData(data);
	    boolean test=false;
	    Keyvalue kv=keyvalueService.get(Keys.TestDev);
	    if(null!=kv&&1==kv.intValue()){
	    	test=true;
	    }
	    
	    if(null!=data&&!data.isEmpty()){
	    	for(BcAddr bca:data){
	    		double balance=ETHUtil.getTokenBalance(bca.getAddr(),test);
	    		bca.setBalance(balance);
	    		String ethbalance=ETHUtil.getETHBalance(bca.getAddr());
	    		bca.setEthbalance(ethbalance);
	    	}
	    }
	    
		return success(page);
	}
	
	@ResponseBody
	@RequestMapping(value="query",method=RequestMethod.POST)
	public ActionResult query(HttpServletRequest request){
		ActionValues values=new ActionValues(request);
		return success(bcAddrService.query(values));
	}
     

}